﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using crdmcgui;

namespace crdmcgui
{
    public class UserInfo
    {
        public List<Template> RuleTemplates { get; set; }
        public List<Template> ActionTemplates { get; set; }
        Dictionary<string, string> KeyStorage { get; set; }

        public UserInfo()
        {
            RuleTemplates = new List<Template>();
            ActionTemplates = new List<Template>();
        }

        public UserInfo(string strFilePath)
            : this()
        {
            ReadFile(strFilePath);
        }

        public void ReadFile(string strFilePath)
        {
            Dictionary<string, string> tmpKeys = ConfigHandler.ReadAllKeys(strFilePath);
            KeyStorage = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> item in tmpKeys)
            {
                if (item.Key.StartsWith("RuleTemplate_")) RuleTemplates.Add(new Template(item.Key.Replace("RuleTemplate_", ""), ParameterType.Rule, item.Value));
                else if (item.Key.StartsWith("ActionTemplate_")) ActionTemplates.Add(new Template(item.Key.Replace("ActionTemplate_", ""), ParameterType.Action, item.Value));
                else KeyStorage.Add(item.Key, item.Value);
            }
        }

        public void WriteFile(string strFilePath)
        {
            Dictionary<string, string> tmpKeys = new Dictionary<string, string>();

            foreach (KeyValuePair<string, string> item in KeyStorage)
            {
                tmpKeys.Add(item.Key, item.Value);
            }
            for (int i = 0; i < RuleTemplates.Count; i++)
            {
                tmpKeys.Add("RuleTemplate_" + RuleTemplates[i].Name, RuleTemplates[i].ToString());
            }
            for (int i = 0; i < ActionTemplates.Count; i++)
            {
                tmpKeys.Add("ActionTemplate_" + ActionTemplates[i].Name, ActionTemplates[i].ToString());
            }
            ConfigHandler.WriteAllKeys(tmpKeys, strFilePath);
        }
    }
}